JavaScript - Runtime Environments

Node.js

About
  • "Run JavaScript everywhere".

  • "Runtime environment".

  • It's the most popular "Runtime environment".

Syntax
  • Executing the program:

node my_script.js

Deno

Setup

Installation
  • irm https://deno.land/install.ps1 | iex

Creating a project
  • deno init

    • A deno.json  file is created to configure your project , and two TypeScript files are created: main.ts  and main_test.ts .

      • The main.ts  file is where you'll write your application code; initially, it will contain a simple program that adds two numbers together.

      • The main_test.ts  file is where you can write tests; initially, it will contain a test for your addition program.

VSCode Environment
  • "You will now get all the benefits of Deno’s LSP, including IntelliSense, code formatting, linting, and more."

  • Setup:

    • In the Extensions tab, search for "Deno" and install the extension by Denoland .

    • Open the Command Palette by pressing Ctrl+Shift+P  and type Deno: Initialize Workspace Configuration .

    • That's it. A file called .vscode/settings.json  will be created in your workspace.

Imports
  • Understanding the imports cache .

    • deno info

      • Shows the cache directory of imports.

    • deno --reload main.ts

      • Re-downloads the dependencies.

  • In Deno, the module system uses ESM  (ECMAScript Modules), which is different from CommonJS  (used in Node.js). Instead of module.exports  and require , you use export  and import .

Installing modules with Deno
  • I don't know when to use this.

  • deno install ?

  • deno install npm:express

  • deno install npm:mongoose

  • deno add ?

Syntax

Running the program
deno main.ts
deno run main.ts
TypeScript
  • Deno will compile your TypeScript code to JavaScript with no extra config needed. Deno can also type check your TypeScript code without requiring a separate type checking tool like tsc .

  • Checks the file for static typing.

deno check module.ts
  • When using the deno run  command, Deno will skip type-checking and run the code directly. To perform a type check of the module before execution occurs, you can use the --check  flag with deno run .

deno run --check module.ts
# or also type check remote modules and npm packages
deno run --check=all module.ts
Watching
  • You can supply the --watch  flag to deno run , deno test , deno compile , and deno fmt  to enable the built-in file watcher.

  • The watcher enables automatic reloading of your application whenever changes are detected in the source files.

Others

Declaration file
  • .d.ts

Bun

  • .